package universum.util;

import universum.Main;
import universum.engine.Universe;
import universum.ui.Controller;
import universum.ui.HeadlessController;
import universum.ui.LocalViewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class GameImitator {
    public GameImitatorParams params;
    public  JFrame   frame;
    private Statistics stats = Statistics.getInstance();
    private Universe _universe;
    public GameImitator() {

    }

    public void Imitate() {
        //stats.addReport("Imitation", 1, Statistics.AggregationType.sum);
        ///_universe = new Universe();
        if (params.headless) {
            HeadlessController ctr = new HeadlessController(params.gameInfo, -1);
            java.util.List<String[]> hargs = new ArrayList<String[]>();
            if (params.headlessArgs != null) {
                StringTokenizer st = new StringTokenizer(params.headlessArgs, ", ");
                while (st.hasMoreTokens()) {
                    hargs.add(Main.parseBeing(st.nextToken()));
                }
                params.beingList = hargs.toArray(new String[hargs.size()][]);
            }
            ctr.startGame(params.beingList);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createMainFrame();
                }
            });
        }
    }                                               

    private JComponent createCtr() {
        if (params.viewFile != null) {
            try {
                return new LocalViewer(params.viewFile, 8889);
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            return new Controller(frame, params.gameInfo, params.beingList);
        }
    }

    private void createMainFrame() {
        if (params.altui) {
            universum.ui.sam.Application app =
                    new universum.ui.sam.Application();
            app.run();
            this.frame = app.getFrame();
            return;
        }

        frame = new JFrame("Universum");
        final int WIDTH = 900, HEIGHT = 600;
        frame.setSize(WIDTH, HEIGHT);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width / 2 - WIDTH / 2, d.height / 2 - HEIGHT / 2);

        final JComponent ctr = createCtr();

        frame.addWindowListener(new WindowAdapter() {
            public void windowOpening(WindowEvent e) {
                ctr.repaint();
            }

            public void windowClosing(WindowEvent e) {
                frame.dispose();
                System.exit(0);
            }
        });


        Container c = frame.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(ctr, BorderLayout.CENTER);
        frame.validate();
        frame.setVisible(true);
        frame.repaint();
    }
    // TODO: refactor HeadlessController & Controller to take Universe from here (GameImitator).
    //private
}
